﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:c="www.contoso.com"
    xmlns:aw="http://www.adventure-works.com"
    xmlns:po="http://derrivedoutput.scryber.co.uk/purchase-orders"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl aw c"
>
  <xsl:output method="xml" indent="yes"  />

  <xsl:param name="Min-Year" >0</xsl:param>
  
  <xsl:template match="c:PurchaseOrders" >
    <po:PurchaseOrders >
      <xsl:apply-templates />
    </po:PurchaseOrders>
  </xsl:template>

  <xsl:template match="c:PurchaseOrder" >
    <xsl:if test="number(substring(@OrderDate,1,4)) &gt;= $Min-Year" >
      <po:PurchaseOrder>
        <xsl:attribute name="source" >Contoso</xsl:attribute>
        <xsl:attribute name="po-num">
          <xsl:value-of select="@PurchaseOrderNumber"/>
        </xsl:attribute>
        <xsl:attribute name="order-date" >
          <xsl:value-of select="@OrderDate"/>
        </xsl:attribute>
        <po:Addresses>
          <xsl:apply-templates select="c:Address" />
        </po:Addresses>
        <po:PurchaseItems>
          <xsl:apply-templates select="c:Items/c:Item" />
        </po:PurchaseItems>
        <xsl:apply-templates select="c:DeliveryNotes" />
      </po:PurchaseOrder>
    </xsl:if>
  </xsl:template>

  <xsl:template match="c:Address" >
    <po:Address >
      <xsl:attribute name="type" >
        <xsl:value-of select="@Type"/>
      </xsl:attribute>
      <po:Name>
        <xsl:value-of select="c:Name"/>
      </po:Name>
      <po:Street>
        <xsl:value-of select="c:Street"/>
      </po:Street>
      <po:City>
        <xsl:value-of select="c:City"/>
      </po:City>
      <po:State>
        <xsl:value-of select="c:State"/>
      </po:State>
      <po:Zip>
        <xsl:value-of select="c:Zip"/>
      </po:Zip>
      <po:Country>
        <xsl:value-of select="c:Country"/>
      </po:Country>
    </po:Address>
  </xsl:template>

  <xsl:template match="c:Item" >
    <po:Item num="{@PartNumber}">
      <po:Name>
        <xsl:value-of select="c:ProductName"/>
      </po:Name>
      <po:Quantity>
        <xsl:value-of select="c:Quantity"/>
      </po:Quantity>
      <po:UnitPrice currency="USD" >
        <xsl:value-of select="c:USPrice"/>
      </po:UnitPrice>
      <po:TotalPrice currency="USD" >
        <xsl:value-of select="c:USPrice * c:Quantity"/>
      </po:TotalPrice>
      <xsl:if test="c:ShipDate" >
        <po:ShipDate>
          <xsl:value-of select="c:ShipDate"/>
        </po:ShipDate>
      </xsl:if>
      <xsl:if test="c:Comment" >
        <po:Comment>
          <xsl:value-of select="c:Comment"/>
        </po:Comment>
      </xsl:if>
    </po:Item>
    
  </xsl:template>

  <xsl:template match="c:DeliveryNotes">
    <po:Notes>
      <xsl:value-of select="text()"/>
    </po:Notes>
  </xsl:template>
  
  <!-- Adventure works -->

  <xsl:template match="aw:PurchaseOrder" >
    <xsl:if test="number(substring(@Date,1,4)) &gt;= $Min-Year" >
      <po:PurchaseOrder>
        <xsl:attribute name="source" >Adventure Works</xsl:attribute>
        <xsl:attribute name="po-num">
          <xsl:value-of select="@PONumber"/>
        </xsl:attribute>
        <xsl:attribute name="order-date" >
          <xsl:value-of select="@Date"/>
        </xsl:attribute>
        <po:Addresses>
          <xsl:for-each select="aw:ShippingAddress" >
            <xsl:call-template name="AW-Address" >
              <xsl:with-param name="Type" >Shipping</xsl:with-param>
            </xsl:call-template>
          </xsl:for-each>
          <xsl:for-each select="aw:ShippingAddress" >
            <xsl:call-template name="AW-Address" >
              <xsl:with-param name="Type" >Billing</xsl:with-param>
            </xsl:call-template>
          </xsl:for-each>
        </po:Addresses>
        <po:PurchaseItems>
          <xsl:apply-templates select="aw:Item" ></xsl:apply-templates>
        </po:PurchaseItems>
        <xsl:apply-templates select="aw:DeliveryInstructions"/>
      </po:PurchaseOrder>
    </xsl:if>
  </xsl:template>

  <xsl:template name="AW-Address" >
    <xsl:param name="Type" />
    <po:Address type="{$Type}" >
      <po:Name>
        <xsl:value-of select="aw:Name"/>
      </po:Name>
      <po:Street>
        <xsl:value-of select="aw:Street"/>
      </po:Street>
      <po:City>
        <xsl:value-of select="aw:City"/>
      </po:City>
      <po:State>
        <xsl:value-of select="aw:State"/>
      </po:State>
      <po:Zip>
        <xsl:value-of select="aw:Zip"/>
      </po:Zip>
      <po:Country>
        <xsl:value-of select="aw:Country"/>
      </po:Country>
    </po:Address>
  </xsl:template>

  <xsl:template match="aw:Item" >
    <po:Item num="{@PartNum}" >
      <po:Name>
        <xsl:value-of select="aw:ProductID"/>
      </po:Name>
      <po:Quantity>
        <xsl:value-of select="aw:Qty"/>
      </po:Quantity>
      <po:UnitPrice currency="USD" >
        <xsl:value-of select="aw:Price"/>
      </po:UnitPrice>
      <po:TotalPrice currency="USD" >
        <xsl:value-of select="aw:Price * aw:Qty"/>
      </po:TotalPrice>
    </po:Item>
  </xsl:template>

  <xsl:template match="aw:DeliveryInstructions" >
    <po:Notes>
      <xsl:value-of select="text()"/>
    </po:Notes>
  </xsl:template>

</xsl:stylesheet>
